<?php

	require_once('../inc/fen_fns.php');
	
	sec_session_start_ajax();
	
	$did = $_GET['did'];
	$yid = $_GET['yid'];
	
	if (login_check($mysqli, '>', '50') == true) {

?>

<script src="scripts/main.js" type="text/javascript"></script>
	<div id="users"></div>
						<script type="text/javascript">


var oTable;

$(document).ready(function() {

	oTable = $('#example').dataTable( {
		"sPaginationType": "full_numbers",
		"sDom": 'f<"toolbar">Trtlip',
		"oTableTools": {
			"aButtons": [
				"xls"
			]
		}
	} );
	
	$("div.toolbar").html('');

	
} );

function showUser(obj) {  

	jQuery(document).ready(function($) {
		  $('a[rel*=facebox]').facebox() 

		  jQuery.facebox({ ajax: 'applications/application-detailed.php?pid='+obj })
	}) 
} 

function showHistory(obj) {  

	jQuery(document).ready(function($) {
		  $('a[rel*=facebox]').facebox() 

		  jQuery.facebox({ ajax: 'forms/transaction-history.php?pid='+obj })
	}) 
} 

</script>

			<div class="showtableusers">

			<form id="form">
				<table cellpadding="0" cellspacing="0" border="0" class="display" id="example">
						<thead>
							<tr>
								<th>#</th>
								<th>Müracaat Tarihi</th>
								<th>Ünvan</th>
								<th>Adı</th>							
								<th>Soyadı</th>
								<th>Fakülte</th>
								<th>Bölüm</th>
								<th>Yayın Tipi</th>
								<th>Eser Adı</th>
								<th>İndeks</th>
								<th>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Durum&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</th>
								<th>Tarihçe</th>
<?php 
	if ($_SESSION['access_level'] > 50) { echo "<th>İşlem</th>"; } else { echo "<th>Detay</th>"; }
?>								
								
								
							</tr>
						</thead>
						<tbody>
						
<?php 

				if ($_SESSION['access_level'] >= 100) {
					
					if ($yid == '1') {
						$st = "> '2' ORDER BY status, fen_publication_app.date_added ASC";
					} else if ($yid == '2') {
						$st = "< '3' ORDER BY status, fen_publication_app.date_added DESC";
					}
						
					$query = $_DB->Query("SELECT
											fen_publication_app.id as id,
											fen_publication_app.status as status,
											fen_publication_app.cancel_reason as cancel_reason,
											date_FORMAT(fen_publication_app.date_added , '%Y/%m/%d') as date_added,
											fen_members.titr as titr,
											fen_members.name as name,
											fen_members.surname as surname,
											fen_members.username as username,
											fen_department.department_name as department_name,
											fen_faculty.faculty_name as faculty_name,
											fen_publications.eser_adi as publication_name,
											fen_publications.indeks_turu as indeks_turu,
											fen_publications.sinif as sinif,
											fen_publications.yayin_tipi as yayin_tipi,
											fen_publications.id as publication_id
											from fen_publication_app, fen_members, fen_department, fen_faculty, fen_publications
											WHERE
											fen_publication_app.member_id = fen_members.id and
											fen_members.department_id = fen_department.id and
											fen_members.faculty_id = fen_faculty.id and
											fen_publication_app.publication_id = fen_publications.id and
											fen_publication_app.status ".$st."
											");
					
					} else if ($_SESSION['access_level'] >= 70) {
						
					$query = $_DB->Query("SELECT
											fen_publication_app.id as id,
											fen_publication_app.status as status,
											fen_publication_app.cancel_reason as cancel_reason,
											date_FORMAT(fen_publication_app.date_added , '%Y/%m/%d') as date_added,
											fen_members.titr as titr,
											fen_members.name as name,
											fen_members.surname as surname,
											fen_members.username as username,
											fen_department.department_name as department_name,
											fen_faculty.faculty_name as faculty_name,
											fen_publications.eser_adi as publication_name,
											fen_publications.indeks_turu as indeks_turu,
											fen_publications.sinif as sinif,
											fen_publications.yayin_tipi as yayin_tipi,
											fen_publications.id as publication_id
											from fen_publication_app, fen_members, fen_department, fen_faculty, fen_publications
											WHERE
											fen_publication_app.member_id = fen_members.id and
											fen_members.department_id = fen_department.id and
											fen_members.faculty_id = fen_faculty.id and
											fen_publication_app.publication_id = fen_publications.id and
											fen_publication_app.status > 3
											ORDER BY date_added DESC");
					
					} else if ($_SESSION['access_level'] >= 60) {
						
					$query = $_DB->Query("SELECT
											fen_publication_app.id as id,
											fen_publication_app.status as status,
											fen_publication_app.cancel_reason as cancel_reason,
											date_FORMAT(fen_publication_app.date_added , '%Y/%m/%d') as date_added,
											fen_members.titr as titr,
											fen_members.name as name,
											fen_members.surname as surname,
											fen_members.username as username,
											fen_department.department_name as department_name,
											fen_faculty.faculty_name as faculty_name,
											fen_publications.eser_adi as publication_name,
											fen_publications.indeks_turu as indeks_turu,
											fen_publications.sinif as sinif,
											fen_publications.yayin_tipi as yayin_tipi,
											fen_publications.id as publication_id
											from fen_publication_app, fen_members, fen_department, fen_faculty, fen_publications
											WHERE
											fen_publication_app.member_id = fen_members.id and
											fen_members.department_id = fen_department.id and
											fen_members.faculty_id = fen_faculty.id and
											fen_publication_app.publication_id = fen_publications.id
											ORDER BY date_added DESC");
					
					} else if ($_SESSION['access_level'] >= 50) {
						
						$query = $_DB->Query("SELECT
											fen_publication_app.id as id,
											fen_publication_app.status as status,
											fen_publication_app.cancel_reason as cancel_reason,
											date_FORMAT(fen_publication_app.date_added , '%Y/%m/%d') as date_added,
											fen_members.titr as titr,
											fen_members.name as name,
											fen_members.surname as surname,
											fen_members.username as username,
											fen_department.department_name as department_name,
											fen_faculty.faculty_name as faculty_name,
											fen_publications.eser_adi as publication_name,
											fen_publications.indeks_turu as indeks_turu,
											fen_publications.sinif as sinif,
											fen_publications.yayin_tipi as yayin_tipi,
											fen_publications.id as publication_id
											from fen_publication_app, fen_members, fen_department, fen_faculty, fen_publications
											WHERE
											fen_publication_app.member_id = fen_members.id and
											fen_members.department_id = fen_department.id and
											fen_members.faculty_id = fen_faculty.id and
											fen_faculty.id = (select faculty_id from fen_members where id = '".$_SESSION['user_id']."') and
											fen_publication_app.publication_id = fen_publications.id
											ORDER BY date_added DESC");
					}
		
					
						$count = $_DB->GetResultNumber($query);

						for ($i=0; $i<$count; $i++)
						{
							
							/*
							if ($_DB->GetResultValue($query, $i, "titr") == 'Prof. Dr.') {
								$titr = "<p id=\"hiddentitr\">1</p>Prof. Dr.";
							} else if ($_DB->GetResultValue($query, $i, "titr") == 'Doç. Dr.') {
								$titr = "<p id=\"hiddentitr\">2</p>Doç. Dr.";
							} else if ($_DB->GetResultValue($query, $i, "titr") == 'Yrd. Doç. Dr.') {
								$titr = "<p id=\"hiddentitr\">3</p>Yrd. Doç. Dr.";
							} else if ($_DB->GetResultValue($query, $i, "titr") == 'Öğr. Gör. Dr.') {
								$titr = "<p id=\"hiddentitr\">4</p>Öğr. Gör. Dr.";
							} else if ($_DB->GetResultValue($query, $i, "titr") == 'Öğr. Gör.') {
								$titr = "<p id=\"hiddentitr\">5</p>Öğr. Gör.";
							} else if ($_DB->GetResultValue($query, $i, "titr") == 'Uzm.') {
								$titr = "<p id=\"hiddentitr\">6</p>Uzm.";
							} else if ($_DB->GetResultValue($query, $i, "titr") == 'Okutman') {
								$titr = "<p id=\"hiddentitr\">7</p>Okutman";
							} else if ($_DB->GetResultValue($query, $i, "titr") == 'Arş. Gör.') {
								$titr = "<p id=\"hiddentitr\">8</p>Arş. Gör.";
							}
							*/
							$titr = $_DB->GetResultValue($query, $i, "titr");
							
							if ($_DB->GetResultValue($query, $i, "sinif")) {
								$sinif = "<br>(".$_DB->GetResultValue($query, $i, "sinif").")";
							} else {
								$sinif = "";
							}
							
							
								
							
							if ($_DB->GetResultValue($query, $i, "status") == '0') {
								$status = "<a class=\"tooltip\" title=\"".$_DB->GetResultValue($query, $i, "cancel_reason")."\"><font color=\"#dd0303\">Reddedildi</font><img src=\"images/reasonicon.gif\"></img></a>";
							} else if ($_DB->GetResultValue($query, $i, "status") == '2') {
								$status = "<font color=\"#f97b00\">Yeni</font>";
							} else if ($_DB->GetResultValue($query, $i, "status") == '3') {
								if ($_SESSION['access_level'] == '100') {
									$status = "<font color=\"#539e3b\">Bekliyor</font>";
								} else {
									$status = "<font color=\"#539e3b\">Rektörlüğe Gönderildi</font>";
								}
							} else if ($_DB->GetResultValue($query, $i, "status") == '4') {
								
								if ($_SESSION['access_level'] == '70') {
									$status = "<font color=\"#8066a0\">Bekliyor</font>";
								} else {
									$status = "<font color=\"#8066a0\">Mali İşlere Gönderildi</font>";
								}
								
							} else if ($_DB->GetResultValue($query, $i, "status") == '5') {
								$queryamount = $_DB->Query("SELECT amount, date_FORMAT(date_added , '%d/%m/%Y') as date_added from fen_payments where publication_id = '".$_DB->GetResultValue($query, $i, "publication_id")."'");
								$status = "<a class=\"tooltip\" title=\"".$_DB->GetResultValue($queryamount, "0", "date_added")." tarihinde<br> ".$_DB->GetResultValue($queryamount, "0", "amount")." TL ödendi.\"><font color=\"#35aed5\">Ödeme Yapıldı</font><img src=\"images/reasonicon.gif\"></img></a>";
							}
							
							$no = $i + 1;
							
							
							echo "
								<tr>
									<td>".$no."</td>
									<td>".$_DB->GetResultValue($query, $i, "date_added")."</td>
									<td>".$titr."</td>
									<td>".$_DB->GetResultValue($query, $i, "name")."</td>
									<td>".$_DB->GetResultValue($query, $i, "surname")."</td>
									<td>".$_DB->GetResultValue($query, $i, "faculty_name")."</td>
									<td>".$_DB->GetResultValue($query, $i, "department_name")."</td>
									<td>".$_DB->GetResultValue($query, $i, "yayin_tipi")."</td>
									<td>".$_DB->GetResultValue($query, $i, "publication_name")."</td>
									<td>".$_DB->GetResultValue($query, $i, "indeks_turu")." ".$sinif."</td>
									<td><font color=\"#f97b00\">".$status."</font></td>
									<td class=\"center\"><a class =\"user_detail_but\" onclick=\"showHistory('".$_DB->GetResultValue($query, $i, "publication_id")."')\" href=\"#\"><img id=\"editimg\" src=\"images/transactionhistory.gif\"></img></a></td>";				

									if ($_SESSION['access_level'] >= 50) {
										echo "<td class=\"center\"><a class =\"user_detail_but\" onclick=\"showUser('".$_DB->GetResultValue($query, $i, "id")."')\" href=\"#\"><img id=\"editimg\" src=\"images/edit.gif\"></img></a></td>";
									}
									
							echo "</tr>";		
						}

?>
						</tbody>
					</table>				
				</form>
			</div>
			<br><br>
<?php 
	} else {
		header('Location: ./index.php?error=2');
	}			
?>